package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.musicserver.Request.SingerRequest;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.Singer;
import org.example.musicserver.mapper.SingerMapper;
import org.example.musicserver.service.SingerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {

    @Resource
    private SingerMapper singerMapper;

    @Override
    public Result allSinger(String query) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        if(query != null){
            queryWrapper.like("name",query);
        }
        return Result.success(null, singerMapper.selectList(queryWrapper));
    }
    @Override
    public Result singerOfId(Integer id){
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return Result.success(null, singerMapper.selectOne(queryWrapper));
    }

    @Override
    public Result deleteSinger(Integer id){
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        if (singerMapper.delete(queryWrapper) > 0) {
            return Result.success("删除歌手成功", true);
        } else {
            return Result.error("删除歌手失败");
        }
    }

    //根据列表删除多
    @Override
    public Result deleteSingers(@RequestParam("singerIds[]") List<Integer> singerIds){
        int deletedCount = singerMapper.deleteBatchIds(singerIds);
        if (deletedCount > 0) {
            return Result.success("删除歌单成功", true);
        } else {
            return Result.error(deletedCount+"删除歌单失败");
        }
    }
    @Override
    public Result addSinger(SingerRequest addSingerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(addSingerRequest, singer);
        String singerPic = "/img/avatorImages/user.jpg";
        Byte areaType = 2;
        singer.setAreaType(areaType);
        singer.setSingerPic(singerPic);
        if (singerMapper.insert(singer) > 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result updateSinger(SingerRequest updateRequest){
        Singer singer = new Singer();
        BeanUtils.copyProperties(updateRequest, singer);
        if (singerMapper.updateById(singer) > 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

}
